/* 
 * 创建日期 2011-3-28
 *
 * 成都天和软件公司
 * 电话：028-85425861 
 * 传真：028-85425861-8008 
 * 邮编：610041 
 * 地址：成都市武侯区航空路6号丰德万瑞中心B座1001 
 * 版权所有
 */
package  org.fantasy.cpp.core.service.impl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import  org.fantasy.cpp.core.dao.ChartConfigDAO;
import  org.fantasy.cpp.core.dao.ItemAttrDAO;
import  org.fantasy.cpp.core.dao.QueryConfigDAO;
import  org.fantasy.cpp.core.dao.QueryItemDAO;
import  org.fantasy.cpp.core.dao.QueryPageDAO;
import  org.fantasy.cpp.core.dao.QueryTempletDAO;
import  org.fantasy.cpp.core.dao.SourceInfoDAO;
import  org.fantasy.cpp.core.dao.TableConfigDAO;
import  org.fantasy.cpp.core.dao.nutz.BaseDAO;
import  org.fantasy.cpp.core.pojo.ChartConfig;
import  org.fantasy.cpp.core.pojo.ItemAttr;
import  org.fantasy.cpp.core.pojo.QueryConfig;
import  org.fantasy.cpp.core.pojo.QueryItem;
import  org.fantasy.cpp.core.pojo.QueryPage;
import  org.fantasy.cpp.core.pojo.QueryTemplet;
import  org.fantasy.cpp.core.pojo.SourceInfo;
import  org.fantasy.cpp.core.pojo.TableConfig;
import  org.fantasy.cpp.core.service.PageService;
import  org.fantasy.common.cache.service.CacheService;

/**
 * 页面服务
 * 
 * @author: 王文成
 * @version: 1.0
 * @since 2011-3-11
 */
@Service("pageService")
public class PageServiceImpl implements PageService {

	@Autowired
	private TableConfigDAO tableConfigDAO;

	@Autowired
	private QueryConfigDAO queryConfigDAO;

	@Autowired
	private QueryPageDAO queryPageDAO;

	@Autowired
	private QueryItemDAO queryItemDAO;

	@Autowired
	private ItemAttrDAO itemAttrDAO;

	@Autowired
	private SourceInfoDAO sourceInfoDAO;

	@Autowired
	private QueryTempletDAO queryTempletDAO;

	@Autowired
	private ChartConfigDAO chartConfigDAO;

	@Autowired
	@Qualifier("queryConfigCacheService")
	private CacheService cache;

	@Autowired
	private BaseDAO baseDAO;

	/**
	 * 取得查询页面信息
	 * 
	 * @param pageId
	 * @return
	 * @throws Exception
	 */
	public QueryPage getQueryPage(Long pageId) throws Exception {
		QueryPage page = (QueryPage) cache.get(pageId.toString());
		if (page == null) {
			// 页面信息
			page = queryPageDAO.getQueryPage(pageId);
			// 子页面信息
			List<QueryPage> childPages = getChildPages(pageId);
			page.setChildPages(childPages);
			// 表头配置
			TableConfig table = tableConfigDAO.getTableConfig(page.getTableId());
			page.setTableConfig(table);
			// 查询项
			List<QueryItem> items = getQueryItemByPageId(pageId);
			page.setQueryItems(items);
			// 查询模板
			QueryTemplet templet = queryTempletDAO.getQueryTemplet(page.getTempletId());
			page.setQueryTemplate(templet);
			// 查询配置
			QueryConfig query = getQueryConfig(page.getQueryId());
			page.setQueryConfig(query);
			// 图表配置
			ChartConfig chart = chartConfigDAO.getChartConfig(page.getChartId());
			page.setChartConfig(chart);
			cache.put(pageId.toString(), page);
		}
		return page;
	}

	public List<QueryPage> getChildPages(Long pageId) {
		return queryPageDAO.getChildPages(pageId);
	}

	/**
	 * 取得模块信息
	 */
	public TableConfig getTableConfig(Long tableId) throws Exception {
		return tableConfigDAO.getTableConfig(tableId);
	}

	/**
	 * 取得查询项
	 * 
	 * @param moduleId
	 * @return
	 * @throws Exception
	 */
	public List<QueryItem> getQueryItemByPageId(Long pageId) throws Exception {
		List<QueryItem> items = queryItemDAO.getQueryItemByPageId(pageId);
		for (QueryItem item : items) {
			// 扩展属性
			item.setAttrs(getItemAttrs(item.getItemId()));
			// 查询信息
			if (item.getQueryId() != null)
				item.setQueryConfig(getQueryConfig(item.getQueryId()));
		}
		return items;
	}

	/**
	 * 取得扩展属性
	 * 
	 * @param itemId
	 * @return
	 * @throws Exception
	 */
	public List<ItemAttr> getItemAttrs(Long itemId) throws Exception {
		return itemAttrDAO.getItemAttrByItemdId(itemId);
	}

	/**
	 * 取得扩展属性
	 * 
	 * @param itemId
	 * @return
	 * @throws Exception
	 */
	public SourceInfo getSourceInfo(Long sourceId) throws Exception {
		return sourceInfoDAO.getSourceInfo(sourceId);
	}

	public QueryConfig getQueryConfig(Long queryId) throws Exception {
		QueryConfig config = queryConfigDAO.getQueryConfig(queryId);
		if (config != null && config.getSourceId() != null) {
			SourceInfo sourceInfo = sourceInfoDAO.getSourceInfo(config.getSourceId());
			config.setSourceInfo(sourceInfo);
		}
		return config;
	}

	@Override
	public List<QueryConfig> getQueryConfigList(int configTypeId) throws Exception {
		Cnd cnd = Cnd.where("configTypeId", "=", configTypeId);
		List<QueryConfig> list = baseDAO.query(QueryConfig.class, cnd, null);
		for(QueryConfig config : list){
			Long sourceId = config.getSourceId();
			if( sourceId != null ){
				SourceInfo sourceInfo = getSourceInfo(sourceId);
				config.setSourceInfo(sourceInfo);
			}
		}
		return list;
	}

	public ChartConfig getChartConfig(Long chartId) throws Exception {
		return chartConfigDAO.getChartConfig(chartId);
	}
}
